﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using FarseerPhysics.Common;
using FarseerPhysics.Collision.Shapes;

namespace BHG.System
{
    abstract class GameObject : AbstractObject
    {
        public Body body { get; internal set; }
        public override float Rotation { get { return body.Rotation; } set { body.Rotation = value; } }
        public override Vector2 Position { get { return body.Position; } set { body.Position = value; } }

        /* inheritance only */
        protected GameObject() { }

        public GameObject(Vector2 pos, float xRot, bool dynamic, Model model, Shape shape):base(model)
        {
            physicsAdd(pos, shape, xRot, dynamic);
        }

        public GameObject(Vector2 pos, float xRot, bool dynamic, Model model, Shape shape, float scale)
            : base(model, scale)
        {
            physicsAdd(pos, shape, xRot, dynamic);
        }

        public GameObject(Vector2 pos, float xRot, bool dynamic, Model model, float mass, Shape shape)
            : base(model)
        {
            physicsAdd(pos, shape, xRot, dynamic, mass);
        }

        public void physicsAdd(Vector2 pos, Shape bodyShape, float xRot, bool dynamic)
        {
            body = BodyFactory.CreateBody(PhysicsEngine.Instance.gameWorld);
            body.CreateFixture(bodyShape);
            body.BodyType = (dynamic == true ? BodyType.Dynamic : BodyType.Static);

            body.Position = pos;
            body.Rotation = xRot;

            body.UserData = this;
        }

        public void physicsAdd(Vector2 pos, Shape bodyShape, float xRot, bool dynamic, float mass)
        {
            physicsAdd(pos, bodyShape, xRot, dynamic);
            body.Mass = mass;
        }

        public override void doRemove() {
            base.doRemove();
            if ( this.body == null ) return;
            PhysicsEngine.Instance.removeObjectFromPhysics(this);
        }
    }
}
